package com.batchengine.db;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * <code>QueryLoader</code> is a registry for sets of queries so
 * that multiple copies of the same queries aren't loaded into memory.
 * This implementation loads properties files filled with query name to
 * SQL mappings.  This class is thread safe.
 */
public class QueryLoader {

    /**
     * The Singleton instance of this class.
     */
    private static final QueryLoader instance = new QueryLoader();

    /**
     * Return an instance of this class.
     *
     * @return The Singleton instance.
     */
    public static QueryLoader instance() {
        return instance;
    }

    /**
     * Maps query set names to Maps of their queries.
     */
    private Map<String, Map<Object, Object>> queries = new HashMap<String, Map<Object, Object>>();

    /**
     * QueryLoader constructor.
     */
    protected QueryLoader() {
        super();
    }

    /**
     * Loads a Map of query names to SQL values.  The Maps are cached so a
     * subsequent request to load queries from the same path will return
     * the cached Map.
     *
     * @param path The path that the ClassLoader will use to find the file.
     *             This is <strong>not</strong> a file system path.  If you had a jarred
     *             Queries.properties file in the com.yourcorp.app.jdbc package you would
     *             pass "/com/yourcorp/app/jdbc/Queries.properties" to this method.
     * @return Map of query names to SQL values
     * @throws IOException              if a file access error occurs
     * @throws IllegalArgumentException if the ClassLoader can't find a file at
     *                                  the given path.
     */
    public synchronized Map<Object, Object> load(String path) throws IOException {

        Map<Object, Object> queryMap = this.queries.get(path);

        if (queryMap == null) {
            queryMap = this.loadQueries(path);
            this.queries.put(path, queryMap);
        }

        return queryMap;
    }

    /**
     * Loads a set of named queries into a Map object.  This implementation
     * reads a properties file at the given path.
     *
     * @param path The path that the ClassLoader will use to find the file.
     * @return Map of query names to SQL values
     * @throws IOException              if a file access error occurs
     * @throws IllegalArgumentException if the ClassLoader can't find a file at
     *                                  the given path.
     * @since DbUtils 1.1
     */
    protected Map<Object, Object> loadQueries(String path) throws IOException {
        InputStream in = getClass().getResourceAsStream(path);

        if (in == null) {
            throw new IllegalArgumentException(path + " not found.");
        }

        Properties props = new Properties();
        props.load(in);

        // Copy to HashMap for better performance
        return new HashMap<Object, Object>(props);
    }

    /**
     * Removes the queries for the given path from the cache.
     *
     * @param path The path that the queries were loaded from.
     */
    public synchronized void unload(String path) {
        this.queries.remove(path);
    }

}
